package com.spaceraccoons.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Years;

import com.google.appengine.api.datastore.Key;


/**
 * 
 * @author Benjamin P. Jung
 * @version 1.0
 */
@Entity(name="user")
@Table(name="user", uniqueConstraints={})
public class User implements java.io.Serializable {

	/** @see java.io.Serializable */
	private static final long serialVersionUID = 3424331371590775077L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	Key key;

	@Column(name="first_name", length=100)
	private String firstName;

	@Column(name="middle_name", length=100, nullable=true)
	private String middleName;

	@Column(name="last_name", length=100)
	private String lastName;
	
	@Column(name="nickname", length=50)
	private String nickname;

	@Column(name="gender", nullable=false)
	private Gender gender;

	@Column(name="birthday", nullable=true)
	private DateMidnight birthday;

	// ---- GETTERS AND SETTERS ------------------------------------------------

	
	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(@NotNull final String firstName) {
		this.firstName = firstName;
	}
	
	public String getMiddleName() {
		return this.middleName;
	}
	
	public void setMiddleName(final String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(@NotNull final String lastName) {
		this.lastName = lastName;
	}
	
	public String getNickname() {
		return this.nickname;
	}
	
	public void setNickname(final String nickname) {
		this.nickname = nickname;
	}

	public Gender getGender() {
		return this.gender;
	}
	
	public void setGender(final Gender gender) {
		this.gender = gender;
	}
	
	public DateMidnight getBirthday() {
		return this.birthday;
	}

	public void setBirthday(final DateMidnight birthday) {
		this.birthday = birthday;
	}

	// ---- ADDITIONAL HELPER METHODS ------------------------------------------
	
	/**
	 * @return	The user's age in years.
	 * @throws IllegalStateException	If the birthday of the user has not
	 *                              	been set
	 */
	public Years getAge() {
		if (this.birthday == null) {
			throw new IllegalStateException("Cannot determine user's age, because birthday is unknown.");
		}
		// else
		final DateTime now = new DateTime();
		return Years.yearsBetween(this.birthday, now);
	}

	// ---- OVERRIDEN METHODS --------------------------------------------------
	
	@Override
	public String toString() {
		return String.format("%s %s", this.getFirstName(), this.getLastName());
	}

}
